package com.yxhl.km.module.login.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.WindowManager;

import com.yxhl.km.HomeActivity;
import com.yxhl.km.R;
import com.yxhl.km.base.BaseActivity;
import com.yxhl.km.module.entity.EventBean;
import com.yxhl.km.module.login.entity.UserInfoBean;
import com.yxhl.km.module.login.mvp.LoginPrensenter;
import com.yxhl.km.module.utils.Constants;
import com.yxhl.km.module.utils.MD5Utils;
import com.yxhl.km.module.utils.PicassoUtil;
import com.yxhl.km.module.utils.SharedPreferencesUtils;
import com.yxhl.km.module.utils.ToastUtil;
import com.yxhl.km.module.view.DiagUtil;
import com.yxhl.km.mvp.BaseView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by 张亚楠 on 2018/7/3.
 */

public class WelcomeActivity extends BaseActivity {
    private String account;
    private String password;

    @Override
    protected void initListener() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    protected void initialized(android.os.Bundle savedInstanceState) {

        account = (String) SharedPreferencesUtils.getParam(mContext, Constants.ACCOUNT, "");
        password = (String) SharedPreferencesUtils.getParam(mContext, Constants.PASSWORD, "");
        if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(password)) {
            goLogin();
            return;
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                finish();
            }
        }, 1000);
    }

    private void goLogin() {
        LoginPrensenter loginPrensenter = new LoginPrensenter(new BaseView<UserInfoBean>() {
            @Override
            public void onResult(UserInfoBean userInfoBean) {
                SharedPreferencesUtils.setObjectToShare(mContext, Constants.USERINFO, userInfoBean);
                SharedPreferencesUtils.setParam(mContext, Constants.PASSWORD, password);
                SharedPreferencesUtils.setParam(mContext, Constants.ACCOUNT, account);
                gotoActivity(HomeActivity.class, true, null);
            }
        });
        loginPrensenter.getLoginData(account, password);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_welcome;
    }
}
